package com.first.system.service.Impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.first.common.pojo.AclRole;
import com.first.common.pojo.AclUserRole;
import com.first.system.mapper.RoleMapper;
import com.first.system.mapper.UserRoleMapper;
import com.first.system.service.RoleService;
import com.first.system.vo.AddRoleVo;
import com.first.system.vo.RoleVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: finrsteducation-parent
 * @description:
 * @author: 张玉森
 * @create: 2023-05-26 16:45
 **/
@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private UserRoleMapper userRoleMapper;

    @Override
    public List<AclRole> findAll() {
        QueryWrapper<AclRole> wrapper=new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        return roleMapper.selectList(wrapper);
    }

    @Override
    public List<AclRole> findByUserId(String userId) {
        List<AclRole> byId = roleMapper.findById(userId);
        return byId;
    }

    @Override
    public void confirm(String userId, String[] roleIds) {
        //删除该用户具有的角色
        roleMapper.deleteByUserId(userId);
        //批量添加用户角色
        List<AclUserRole> userRoleList = Arrays.stream(roleIds).map(roleId -> new AclUserRole(
                IdUtil.getSnowflakeNextId() + "", roleId, userId, 0, LocalDateTime.now(),
                LocalDateTime.now())).collect(Collectors.toList());
        // System.out.println(userRoleList);
        userRoleMapper.batchInsert(userRoleList);
    }

    @Override
    public Page<AclRole> findRolePage(String page, String pageSize, RoleVo roleVo) {
        Page<AclRole> Page = new Page<>(Integer.valueOf(page), Integer.valueOf(pageSize));
        QueryWrapper<AclRole> wrapper = new QueryWrapper<>();
        if (roleVo.getRoleName() != null && roleVo.getRoleName() != "") {
            wrapper.like("role_name", roleVo.getRoleName());
        }
        if (roleVo.getIsDeleted() != null && roleVo.getIsDeleted() != "") {
            wrapper.eq("is_deleted", roleVo.getIsDeleted());
        }
        Page<AclRole> aclRolePage = roleMapper.selectPage(Page, wrapper);
        return aclRolePage;
    }

    @Override
    public Integer updateRoleId(String id, String deleted) {
        UpdateWrapper<AclRole> update=new UpdateWrapper<>();
        update.set("is_deleted",deleted).eq("id",id);
        int upd1 = roleMapper.update(null, update);
        QueryWrapper<AclUserRole> wrapper=new QueryWrapper<>();
        wrapper.eq("role_id",id);
        userRoleMapper.delete(wrapper);
        return upd1;
    }

    @Override
    public Integer addUser(AddRoleVo addRoleVo) {
        AclRole role = new AclRole();
            role.setId(RandomUtil.randomNumbers(19));
        role.setRoleName(addRoleVo.getRoleName());
        role.setRoleCode("1");
        role.setRemark(addRoleVo.getRemark());
        role.setIsDeleted(0);
        role.setGmtCreate(LocalDateTime.now());
        role.setGmtModified(LocalDateTime.now());
        Integer insert = roleMapper.insert(role);
        return insert;
    }

    @Override
    public Integer updateUser(AclRole aclRole) {
        return roleMapper.updateById(aclRole);
    }

    @Override
    public Integer deleteRoleById(String id) {
        AclRole aclRole = new AclRole();
        aclRole.setId(id);
        roleMapper.updateById(aclRole);
        return null;
    }
}
